home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / System Extras Headers / GX Printing Libraries / PicturesAndPICTLibrary.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-17  |  5.3 KB  |  187 lines  |  [TEXT/MMCC]

  1. /*
  2.     Libraries for dealing with pictures within PICTs
  3.     copyright © 1992 Apple Computer, Inc.
  4.     All rights reserved.
  5.     
  6.     
  7.     Routines in this library:
  8.         PictureToPICT         - creates a PICT with an embedded picture shape
  9. */
  10. #include <Types.h>
  11. #include <Quickdraw.h>
  12. #include <ToolUtils.h>
  13. #include <Memory.h>
  14.  
  15. #include "graphics routines.h"
  16. #include "graphics toolbox.h"
  17. #include "math routines.h"
  18. #include "storage library.h"
  19. #include "offscreen library.h"
  20. #include "qd library.h"
  21. #include <PrintingLibraries.h>
  22.  
  23. //-----------------------------------------------------------------------------
  24. // INTERNAL TYPEDEFS AND DEFINES
  25. //-----------------------------------------------------------------------------
  26.  
  27. // IDs for the PicComments
  28. #define shapeSignature    'qdgx'
  29. #define shapeBegin        500
  30. #define shapeEnd        501
  31.  
  32. // typedefs for the new PicComments
  33. typedef struct 
  34.     {
  35.     OSType        signature;        // always == shapeSignature
  36.     short        kind;            // always == shapeBegin
  37.     Rect        bounds;            // bounds of shape @ 72 dpi
  38.     char        data[1];        // flattened shape data, total size of record determines
  39.                                 // size
  40.     } ShapeBeginRecord, *ShapeBeginPtr, **ShapeBeginHdl;
  41.     
  42. typedef struct
  43.     {
  44.     OSType        signature;        // always == shapeSignature
  45.     short        kind;            // always == shapeEnd
  46.     } ShapeEndRecord, *ShapeEndPtr, **ShapeEndHdl;
  47.  
  48. //-----------------------------------------------------------------------------
  49.  
  50. PicHandle    PictureToPICT(gxShape theShape, Boolean simpleProxy)
  51. /*
  52.     This library routine turns a QuickDraw GX™ shape into a QuickDraw PICT.
  53.     It does this using three steps:
  54.         1)  it emits a QuickDraw PicComment, and places the flattened GX data into
  55.             the comment.
  56.         2)    it converts the GX picture into a QuickDraw proxy.  It uses a rectangle
  57.             with an 'X' through it if simpleProxy is true.  Otherwise, it uses a 1 bit
  58.             bitmap rendition of the shape.
  59.         3)    it emits a QuickDraw PictComment, marking the end of the QuickDraw proxy.
  60.         
  61.     The resulting PICT can be cut & pasted, or saved into a PICT file.  On a system
  62.     with QuickDraw GX™ installed, the GX data will be used when printing.  
  63.     When printing on other systems, the QuickDraw proxy will be used.
  64.     
  65.     The proxy is always used for display from within non-GX applications.
  66. */
  67. {
  68.     gxGraphicsError        gxErr = noErr;
  69.     PicHandle            thePicture;
  70.     Rect                picRect;
  71.     ShapeBeginRecord    theBegin;
  72.     Handle                hBegin;
  73.     gxRectangle            shapeBounds;
  74.         
  75.     // get the location of the shape
  76.     GXGetShapeDeviceBounds(theShape, 0, 0, &shapeBounds);    
  77.     picRect.left     = FixedToInt(shapeBounds.left);
  78.     picRect.top     = FixedToInt(shapeBounds.top);
  79.     picRect.right     = FixedToInt(shapeBounds.right);
  80.     picRect.bottom     = FixedToInt(shapeBounds.bottom);
  81.     GlobalToLocal((Point*) &picRect.top);
  82.     GlobalToLocal((Point*) &picRect.bottom);
  83.         
  84.     thePicture = OpenPicture(&picRect);
  85.     if (thePicture != nil)
  86.         {
  87.         // use a standard clipping and pen mode
  88.         ClipRect(&picRect);
  89.         PenNormal();
  90.         
  91.         // flatten our shape out into a handle
  92.         hBegin = ShapeToHandle(theShape);
  93.         if (hBegin != nil)
  94.             {
  95.             // add the comment to the begining of the handle
  96.             theBegin.signature     = shapeSignature;
  97.             theBegin.kind         = shapeBegin;
  98.             theBegin.bounds     = picRect;
  99.             Munger(hBegin, 0, nil, 0, &theBegin, sizeof(OSType) + sizeof(short) + sizeof(Rect) );
  100.             gxErr = MemError();
  101.         
  102.             // store the shape/handle into the picture
  103.             PicComment(shapeBegin, GetHandleSize(hBegin), hBegin);
  104.             DisposHandle(hBegin);
  105.             
  106.             // Send the dimensions of the shape at 72 dpi
  107.             PenMode(23);
  108.             FrameRect(&picRect);
  109.             PenNormal();
  110.             }
  111.         else
  112.             gxErr = MemError();
  113.     
  114.         if (gxErr == noErr)
  115.             {
  116.             if (simpleProxy)
  117.                 {
  118.                 // our proxy is just a framed rect with an X through it
  119.                 FrameRect(&picRect);
  120.                 MoveTo(picRect.left,     picRect.top);
  121.                 LineTo(picRect.right,     picRect.bottom);
  122.                 MoveTo(picRect.right,     picRect.top);
  123.                 LineTo(picRect.left,     picRect.bottom);
  124.                 }
  125.             else
  126.                 {
  127.                 // our proxy is a bitmap of the GX object
  128.                 gxBitmap        theBits;
  129.                 gxShape            theBitmap;
  130.  
  131.                 theBits.width         = picRect.right - picRect.left;
  132.                 theBits.height         = picRect.bottom - picRect.top;
  133.                 theBits.pixelSize     = 1;
  134.                 theBits.rowBytes    = ((theBits.width + 31) >> 5) << 2;
  135.                 theBits.image         = NewPtrClear(theBits.height * theBits.rowBytes);
  136.                 theBits.space         = gxIndexedSpace;
  137.                 theBits.set         = nil;
  138.                 theBits.profile     = nil;
  139.                 
  140.                 gxErr = MemError();
  141.                 if (gxErr == noErr)
  142.                     {
  143.                     theBitmap = GXNewBitmap(&theBits, nil);
  144.                     GXMoveTransformTo(GXGetShapeTransform(theBitmap), -shapeBounds.left, -shapeBounds.top);
  145.                     GXGetGraphicsError(&gxErr);
  146.                     if (gxErr == noErr)
  147.                         {
  148.                         BitMap        qdBits;
  149.                         
  150.                         // put the shape into the bitmap
  151.                         CopyToBitmaps(theBitmap, theShape);
  152.                         ConvertToQDBitmap(&theBits, &qdBits);
  153.                         GXGetGraphicsError(&gxErr);
  154.                                                                         
  155.                         // now done with the bitmap
  156.                         GXDisposeShape(theBitmap);
  157.  
  158.                         // CopyBits it into the picture
  159.                         CopyBits(&qdBits, &qd.thePort->portBits, &qdBits.bounds, &picRect, srcOr, nil);
  160.                         
  161.                         }
  162.                         
  163.                     // and done with the image
  164.                     DisposePtr(theBits.image);
  165.                     }
  166.                 }
  167.             
  168.             // mark the end of our shape's proxy
  169.             PicComment(shapeEnd, 0, (Handle) nil);
  170.             }
  171.         
  172.         ClosePicture();
  173.         }
  174.     else
  175.         gxErr = MemError();
  176.         
  177.     // if we had a problem, return a nil picture
  178.     if ( ( gxErr != noErr ) && thePicture )
  179.         {
  180.         KillPicture(thePicture);
  181.         thePicture = nil;
  182.         }
  183.         
  184.     return(thePicture);
  185.     
  186. } // PictureToPICT
  187.